package com.ruoyi.demo.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.ExceptionEnum;
import com.ruoyi.common.exception.LucasAppException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.demo.domain.Todo;
import com.ruoyi.demo.service.ITodoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 演示任务 信息操作处理
 *
 * @author ruoyi
 * @date 2019-05-31
 */
@Controller
@RequestMapping("/demo/todo")
public class TodoController extends BaseController {
	private String prefix = "demo/todo";

	private final ITodoService todoService;

	public TodoController(ITodoService todoService) {
		this.todoService = todoService;
	}

	@RequiresPermissions("demo:todo:view")
	@GetMapping()
	public String todo() {
		return prefix + "/todo";
	}

	/**
	 * 查询演示任务列表
	 */
	@RequiresPermissions("demo:todo:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Todo todo) {
		startPage();
		List<Todo> list = todoService.selectTodoList(todo);
		return getDataTable(list);
	}


	/**
	 * 导出演示任务列表
	 */
	@RequiresPermissions("demo:todo:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(Todo todo) {
		List<Todo> list = todoService.selectTodoList(todo);
		ExcelUtil<Todo> util = new ExcelUtil<>(Todo.class);
		return util.exportExcel(list, "todo");
	}

	/**
	 * 新增演示任务
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存演示任务
	 */
	@RequiresPermissions("demo:todo:add")
	@Log(title = "演示任务", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Todo todo) {
		if (StringUtils.isEmpty(todo.getTitle())) {
			throw new LucasAppException(ExceptionEnum.TODO_NAME_IS_NULL);
		}
		return toAjax(todoService.insertTodo(todo));
	}

	/**
	 * 修改演示任务
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		Todo todo = todoService.selectTodoById(id);
		mmap.put("todo", todo);
		return prefix + "/edit";
	}

	/**
	 * 修改保存演示任务
	 */
	@RequiresPermissions("demo:todo:edit")
	@Log(title = "演示任务", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Todo todo) {
		return toAjax(todoService.updateTodo(todo));
	}

	/**
	 * 删除演示任务
	 */
	@RequiresPermissions("demo:todo:remove")
	@Log(title = "演示任务", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(todoService.deleteTodoByIds(ids));
	}

}
